/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: ${USER}
 * Date: ${YEAR}-${MONTH}-${DAY}
 * Time: ${TIME}
 */
public class Main {
    public static void main(String[] args) {
        System.out.println("Hello world!");
    }
}
class Solution {
    public int[] sortArray(int[] array) {
        heapAdjust (array);
        int end = array.length-1;
        while (end > 0) {
            swap (array,0,end);
            end--;
            siftDown(array,0,end);
        }
        return array;
    }

    private void heapAdjust (int[] array) {

        for (int parent = (array.length-2)/2;parent >= 0;parent--)   {
            siftDown(array,parent,array.length-1);
        }
    }
    //向下调整
    private void siftDown (int[] array,int parent,int end) {
        int child = parent*2+1;
        while (child <= end) {
            if (child+1<=end && array[child+1]>array[child]) {
                child++;
            }
            if (child<=end && array[child]>array[parent]) {
                swap (array,child,parent);
                parent = child;
                child = parent*2+1;
            } else {
                break;
            }
        }
    }

    private void swap (int[] array,int x,int y) {
        int tamp = array[x];
        array[x] = array[y];
        array[y] = tamp;
    }
}